/*******************************************************************************
* Copyright (c) 2010, 2018 西安秦晔信息科技有限公司
* Licensed under the Apache License, Version 2.0 (the "License");
*    you may not use this file except in compliance with the License.
*    You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*    Unless required by applicable law or agreed to in writing, software
*    distributed under the License is distributed on an "AS IS" BASIS,
*    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*    See the License for the specific language governing permissions and
*    limitations under the License.
*******************************************************************************/
package com.qinyeit.serviceapp.service.branchstores.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.qinyeit.serviceapp.entity.Branch;
import com.qinyeit.serviceapp.entity.SyncStockRecord;
import com.qinyeit.serviceapp.entity.enums.SyncStockState;
import com.qinyeit.serviceapp.exception.BusinessRunTimeException;
import com.qinyeit.serviceapp.service.branchs.BranchDataReleasedServiceImpl;
import com.qinyeit.serviceapp.service.branchstores.SyncStockRecordService;
import com.qinyeit.serviceapp.service.organization.BranchService;
import com.qinyeit.serviceapp.utils.SearchUtils;
import com.qinyetech.springstage.core.date.DateUtils;
import com.qinyetech.springstage.core.entity.search.Searchable;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.qinyetech.springstage.core.service.BaseService;
import com.qinyeit.serviceapp.entity.SyncStock;
import com.qinyeit.serviceapp.repository.branchstores.SyncStockRepository;
import com.qinyeit.serviceapp.service.branchstores.SyncStockService;

import java.util.List;
import java.util.Map;

/** 
 * ClassName: SyncStockServiceImpl <br/> 
 * Function: 库存数据下发 业务实现 <br/> 
 * date: 2018-12-12 15:38:46 <br/> 
 * 
 * @author liuwenjin
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@Transactional
public class SyncStockServiceImpl extends BaseService<SyncStock, Long> implements SyncStockService {


	@Autowired
	private SyncStockRepository syncStockRepository;
	@Autowired
	private BranchService branchService;
	@Autowired
	private SyncStockRecordService syncStockRecordService;
	@Autowired
	private BranchDataReleasedServiceImpl branchDataReleasedService;


	@Override
	public void saveData(JSONObject data, Long merchantGroupId) {
		JSONArray branchs = data.getJSONArray("branchs");
		if(CollectionUtils.isEmpty(branchs)){
			throw new BusinessRunTimeException("至少选择一个门店",10001);
		}
		SyncStock syncStock = null;
		SyncStockRecord record = null;
		Branch branch = null;
		List<Long> firstReleased = Lists.newArrayList();
		for (int i = 0; i < branchs.size(); i++) {
			branch = branchService.findById(branchs.getLong(i));
			if(branch==null){
				throw new BusinessRunTimeException("分店不存在",1001);
			}
			//默認是false
			if(!branch.getReleased()){
				firstReleased.add(branch.getMerchantId());
			}
			syncStock = getSyncStock(branchs.getLong(i),merchantGroupId);
			if(syncStock==null){
				syncStock = new SyncStock();
				syncStock.setBranchId(branchs.getLong(i));
				syncStock.setName(branch.getName());
				syncStock.setLastTime(DateUtils.date());
				syncStock.setThisTime(DateUtils.date());
				syncStock.setMerchantGroupId(merchantGroupId);
			}else {
				syncStock.setLastTime(syncStock.getThisTime());
				syncStock.setThisTime(DateUtils.date());
			}
			syncStock.setState(SyncStockState.SUCCESS);
			syncStock = this.update(syncStock);

			record = new SyncStockRecord();
			record.setSyncId(syncStock.getId());
			record.setState(SyncStockState.SUCCESS);
			record.setThisTime(DateUtils.date());
			record.setMerchantGroupId(merchantGroupId);
			syncStockRecordService.save(record);
		}

		//数据下发
		List<Long> bIDs = branchs.toJavaList(Long.class);
		branchDataReleasedService.releasedBranchData(bIDs,merchantGroupId,firstReleased);

	}


	private SyncStock getSyncStock(Long branchId,Long merchantGroupId){
		Searchable searchable = SearchUtils.newSearchableWithMerchantFilter(merchantGroupId);
		searchable.addSearchParam("branchId_eq",branchId);
		SyncStock stocks = this.findOne(searchable);
		return stocks;
	}
}